<template>
  <div style="padding: 20px">
    <el-card>
      <el-row
        type="flex"
        justify="center"
        align="center"
      >
        <el-col>
          <el-row
            type="flex"
            justify="end"
            align="center"
          >
            <el-button
              size="medium"
              type="primary"
              plain
              @click="addNew"
            >创建菜单</el-button>
          </el-row>
        </el-col>
      </el-row>
      <el-table
        :data="list"
        border
        :header-cell-style="{'text-align':'center',background:'#fafafa',color:'#646464',height: '20px'}"
        row-key="id"
        :tree-props="{children: 'children'}"
      >
        <el-table-column
          label="菜单名称"
          prop="permission_name"
          width="200px"
        />
        <el-table-column
          label="菜单地址"
          prop="route"
        />
        <el-table-column
          label="权限标识"
          prop="permission_mark"
        />
        <el-table-column label="图标" align="center">
          <template slot-scope="{row}">
            <svg-icon :icon-class="row.icon" />
          </template>
        </el-table-column>
        <el-table-column
          label="组件路径"
          prop="component"
        />
        <el-table-column label="是否显示">
          <template slot-scope="{row}">
            <el-switch
              v-model="row.hidden"
              :active-value="1"
              :inactive-value="2"
              @change="fn(row)"
            />
          </template>
        </el-table-column>
        <el-table-column
          label="创建时间"
          prop="created_at"
        />
        <el-table-column label="操作">
          <template slot-scope="{row}">
            <el-button
              v-permission="'permission:menu:update'"
              type="primary"
              icon="el-icon-edit"
              circle
              size="medium"
              @click="editMenu(row)"
            />
            <el-button
              type="warning"
              icon="el-icon-plus"
              circle
              @click="addMenuButtonPermission(row.id)"
            />
            <el-button
              type="danger"
              icon="el-icon-delete"
              circle
              size="medium"
              @click="dell(row.id)"
            />
          </template>
        </el-table-column>
      </el-table>
      <!-- <el-row type="flex" justify="end" style="height: 100px" align="middle">
        <el-pagination
          background
          :current-page="currentPage4"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="100"
          layout="total, sizes, prev, pager, next, jumper"
          :total="400"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </el-row> -->
    </el-card>
    <MenuList v-if="matrix" :id="Id" ref="Menu_List" :matrix.sync="matrix" :add-state.sync="addState" :echo.sync="echo" />
  </div>
</template>

<script>
import { getPermissionData, deleteRoleAccordingToId } from '@/api/permission'
import { DisplayStatusAccordingToId } from '@/api/permission_menu'
import MenuList from '@/views/permission/components/menu_list.vue'
export default {
  name: 'Menuu',
  components: {
    MenuList
  },
  data() {
    return {
      list: [], // 表格数据
      matrix: false,
      addState: false,
      Id: null,
      echo: false
    }
  },
  created() {
    this.getPermissionData()
  },
  methods: {
    async getPermissionData() {
      try {
        const { data } = await getPermissionData()
        this.list = data
      } catch (err) {
        console.log(err)
      }
    },
    // 新增菜单
    addNew() {
      this.matrix = true
    },
    // 新增菜单按钮权限
    addMenuButtonPermission(id) {
      this.matrix = true
      this.addState = true
      this.Id = id
    },
    // 编辑事件
    editMenu(row) {
      this.matrix = true
      this.Id = row.id
      this.echo = true
      this.$nextTick(() => {
        if (row.type === 2) {
          this.$refs.Menu_List.tabPosition = 'button'
        }
        this.$refs.Menu_List.echoData(row.id)
      })
    },
    // 删除事件
    async dell(id) {
      await this.$confirm('宁确认要删除吗')
      await deleteRoleAccordingToId(id)
      this.getPermissionData()
    },
    async fn(row) {
      try {
        if (row.hidden === 1) {
          await DisplayStatusAccordingToId(row.id, row.hidden)
        } else if (row.hidden === 2) {
          await DisplayStatusAccordingToId(row.id, row.hidden)
        }
        this.$message.success('操作成功')
      } catch (err) {
        console.log(err)
      }
    }
  }
}
</script>

<style scoped lang="scss">
::v-deep .el-input--small .el-input__inner {
  width: 215px;
  padding-right: 30px
}
::v-deep .el-table__body {
  padding: 0!important;
  height: 30px;
  line-height: 30px!important;
}
::v-deep .el-table tr{
    height: 48px;
  }
::v-deep [data-v-40add2e2] .el-table tr {
  height: 48px;
}
::v-deep .el-link--primary{
  position: relative;
  color: #66b1ff;
  cursor: pointer;
}
::v-deep .el-col-24 {
  height: 60px;
  padding: 10px 0;
}
::v-deep .el-table thead {
  height: 48px;
}
::v-deep .el-table__header tr,
  .el-table__header th {
    padding: 0;
    height: 40px;
}
::v-deep .el-table__body tr,
  .el-table__body td {
    padding: 0;
    height: 40px;
}
</style>
